# -*- coding: UTF-8 -*-
import cgi
import os
from datetime import datetime
from google.appengine.ext import db
from google.appengine.ext.webapp import template
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import images
from google.appengine.api import mail
from google.appengine.ext.webapp.util import login_required
from modules.ds.dsBase import *
from modules.ds.dsCustom import *
#from modules.mail import maillib

#===============================================================================
#    PersonDS = dsEntitySet(primary_key = 'PersonalData')
#    PersonDS.PutAll()
#    IntroDS = dsEntitySet(primary_key = 'IntroData')
#    IntroDS.PutAll()
#    PersonalDS = dsEntitySet(primary_key = 'PersonalInfomation')
#    PersonalDS.Append(PersonDS)
#    PersonalDS.Append(IntroDS)
#    PersonalDS.PutAll()
#===============================================================================


#===============================================================================
#    query = dsEntitySet.gql("WHERE primary_key = 'PersonalInfomation'")
#    if 0 == query.count():
#        self.response.headers['Content-Type'] = 'text/plain'
#        self.response.out.write('Hello, empty World!')
#    else:
#        PersonalDS = query.get()
#        PersonDS = db.get(PersonalDS.entity_keys)
#        myPerson = dsPersonEntity(name = 'Sonic')
#        myPerson.PutAll()
#        PersonDS.Insert(myPerson)
#        myDS.PutAll()    
#===============================================================================
        

class MainPage(webapp.RequestHandler):
  def get(self):
        member_info = ""
        member_link = ""
        if users.get_current_user():
            url = users.create_logout_url(self.request.uri)
            user_obj = PersonalData.PersonalProfile.examineUser(users.get_current_user().email()) 
            if user_obj == 0 or user_obj.certificate == 'false':
                member_info = ""
                member_link = "/apply"
            else:
                member_info = users.get_current_user().nickname() + ""
                member_link = "/profile"
        else:
            url = users.create_login_url(self.request.uri)
        template_values = {
                           'user_obj': users.get_current_user(),
                           'url_link': url,
                           'member_info': member_info,
                           'member_link': member_link,
         }
        path = os.path.join(os.path.dirname(__file__), "templates/index.htm")
        self.response.out.write(template.render(path, template_values))

#===============================================================================
# class Image (webapp.RequestHandler):
#  def get(self):
#      curr_user = PersonalData.PersonalProfile.examineUser(self.request.get("id"))
#      if curr_user == 0:
#          self.response.out.write("No image")
#      else:
#          if curr_user.photo:
#              self.response.headers['Content-Type'] = "image/jpg"
#              self.response.out.write(curr_user.photo)
#          else:
#              self.response.out.write("No image")
#===============================================================================

application = webapp.WSGIApplication([('/', MainPage)],
#===============================================================================
#                                      ('/image', Image)], 
#===============================================================================
                                      debug=True)

def main():
  run_wsgi_app(application)
if __name__ == "__main__":
      main()